//
//  ClinicSearchViewController.m
//  ybzj
//
//  Created by liyuan on 15/7/8.
//  Copyright (c) 2015年 liyuan. All rights reserved.
//

#import "ClinicSearchViewController.h"
#import <CoreText/CoreText.h>


@interface ClinicSearchViewController ()
@property (weak, nonatomic) IBOutlet UISearchBar *ClinicSearchBar;
@property (weak, nonatomic) IBOutlet UIButton *CancelButton;
@property (weak, nonatomic) IBOutlet UILabel *KeyWordLabel;
@property (weak, nonatomic) IBOutlet UIView *TopBarView;
@property (weak, nonatomic) IBOutlet UIView *TopLineView;
@property (nonatomic) NSArray *KeyWordList;

@end

@implementation ClinicSearchViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    self.KeyWordList=[NSArray arrayWithObjects:@"急性胃炎",@"感冒",@"牙疼",@"咳嗽",@"发热",@"膝盖疼",@"颈椎疼",@"发烧",nil];
    //set constrain and color for Top bar
    CGFloat width=[UIScreen mainScreen].bounds.size.width;
    self.TopBarView.backgroundColor=[UIColor colorWithRed:248/255.0 green:248/255.0 blue:249/255.0 alpha:1];
    self.TopLineView.backgroundColor=[UIColor colorWithRed:187/255.0 green:187/255.0 blue:187/255.0 alpha:1];
 
    //topview constrain
    self.TopBarView.translatesAutoresizingMaskIntoConstraints=NO;
    NSLayoutConstraint *topConstrainforTopBarView=[NSLayoutConstraint constraintWithItem:self.TopBarView
                                                                               attribute:NSLayoutAttributeTop
                                                                               relatedBy:NSLayoutRelationEqual
                                                                                  toItem:self.view
                                                                               attribute:NSLayoutAttributeTop
                                                                              multiplier:1.0f
                                                                                constant:0];
    
    topConstrainforTopBarView.active=YES;
    
    //set constrains and color for search view,
    self.ClinicSearchBar.searchBarStyle=UISearchBarStyleMinimal;
    self.ClinicSearchBar.tintColor=[UIColor colorWithRed:0/255.0 green:203/255.0 blue:120/255.0 alpha:1];
    self.ClinicSearchBar.placeholder=@"请输入搜索条件";
    self.ClinicSearchBar.text=self.SearchContent;
    UITextField *searchfield;
    if ([[[UIDevice currentDevice] systemVersion] floatValue]<7.0){
        searchfield=[self.ClinicSearchBar.subviews objectAtIndex:1];
    }
    else{
        searchfield=[((UIView *)[self.ClinicSearchBar.subviews objectAtIndex:0]).subviews lastObject];
    }
    [searchfield setValue:[UIColor colorWithRed:172/255.0 green:172/255.0 blue:177/255.0 alpha:1] forKeyPath:@"_placeholderLabel.textColor"];
    [searchfield setValue:[UIFont systemFontOfSize:width*30/640] forKeyPath:@"_placeholderLabel.font"];
    UIImageView *imagevies;
    ((UILabel *)searchfield.leftView).backgroundColor=[UIColor redColor];
    searchfield.leftView=imagevies;
    [((UIView*)[searchfield.subviews objectAtIndex:0])  removeFromSuperview];
    
    
    //constrain for searchbar'textfield
    searchfield.translatesAutoresizingMaskIntoConstraints=NO;
    NSLayoutConstraint *topConstrainforSearchField=[NSLayoutConstraint constraintWithItem:searchfield
                                                                               attribute:NSLayoutAttributeTop
                                                                               relatedBy:NSLayoutRelationEqual
                                                                                  toItem:self.ClinicSearchBar
                                                                               attribute:NSLayoutAttributeTop
                                                                              multiplier:1.0
                                                                                constant:width*20/640];
    
    NSLayoutConstraint *bottomConstrainforSearchField=[NSLayoutConstraint constraintWithItem:searchfield
                                                                                attribute:NSLayoutAttributeBottom
                                                                                relatedBy:NSLayoutRelationEqual
                                                                                   toItem:self.ClinicSearchBar
                                                                                attribute:NSLayoutAttributeBottom
                                                                               multiplier:1.0
                                                                                 constant:0-width*30/640];
    
    NSLayoutConstraint *leftConstrainforSearchField=[NSLayoutConstraint constraintWithItem:searchfield
                                                                                   attribute:NSLayoutAttributeLeft
                                                                                   relatedBy:NSLayoutRelationEqual
                                                                                      toItem:self.view
                                                                                   attribute:NSLayoutAttributeLeft
                                                                                  multiplier:1.0
                                                                                    constant:0];
    
    NSLayoutConstraint *rightConstrainforSearchField=[NSLayoutConstraint constraintWithItem:searchfield
                                                                                 attribute:NSLayoutAttributeTrailing
                                                                                 relatedBy:NSLayoutRelationEqual
                                                                                    toItem:self.ClinicSearchBar
                                                                                 attribute:NSLayoutAttributeTrailing
                                                                                multiplier:1.0
                                                                                  constant:0];
    
    topConstrainforSearchField.active=YES;
    bottomConstrainforSearchField.active=YES;
    leftConstrainforSearchField.active=YES;
    rightConstrainforSearchField.active=YES;
    
    //self.ClinicSearchBar.backgroundColor=[UIColor blueColor];
    //self.ClinicSearchBar.ti
    
    //sconstrain fro search bar
    self.ClinicSearchBar.translatesAutoresizingMaskIntoConstraints=NO;
    NSLayoutConstraint *topConstrainforClininSearchBar=[NSLayoutConstraint constraintWithItem:self.ClinicSearchBar
                                                                                    attribute:NSLayoutAttributeTop
                                                                                    relatedBy:NSLayoutRelationEqual
                                                                                       toItem:self.view
                                                                                   attribute:NSLayoutAttributeTop
                                                                                   multiplier:1.0f
                                                                                     constant:width*30/640];
    
    NSLayoutConstraint *buttomConstrainforClininSearchBar=[NSLayoutConstraint constraintWithItem:self.ClinicSearchBar
                                                                                    attribute:NSLayoutAttributeBottom
                                                                                    relatedBy:NSLayoutRelationEqual
                                                                                       toItem:self.TopBarView
                                                                                    attribute:NSLayoutAttributeBottom
                                                                                   multiplier:1.0f
                                                                                     constant:0-width*10/640];
    NSLayoutConstraint *leftConstrainforClininSearchBar=[NSLayoutConstraint constraintWithItem:self.ClinicSearchBar
                                                                                       attribute:NSLayoutAttributeLeading
                                                                                       relatedBy:NSLayoutRelationEqual
                                                                                          toItem:self.view
                                                                                       attribute:NSLayoutAttributeLeading
                                                                                      multiplier:1.0f
                                                                                        constant:0];
    NSLayoutConstraint *rightConstrainforClininSearchBar=[NSLayoutConstraint constraintWithItem:self.ClinicSearchBar
                                                                                     attribute:NSLayoutAttributeTrailing
                                                                                     relatedBy:NSLayoutRelationEqual
                                                                                        toItem:self.view
                                                                                     attribute:NSLayoutAttributeTrailing
                                                                                    multiplier:1.0f
                                                                                      constant:0-width*140/640];
    topConstrainforClininSearchBar.active=YES;
    buttomConstrainforClininSearchBar.active=YES;
    leftConstrainforClininSearchBar.active=YES;
    rightConstrainforClininSearchBar.active=YES;
    
    //set constrains and color for cancelbutton
    NSString *sr=@"取消";
    NSMutableAttributedString *SearchString=[[NSMutableAttributedString alloc]  initWithString:sr];
    [SearchString addAttribute:NSForegroundColorAttributeName
                         value:(id)[UIColor colorWithRed:0/255.0 green:203/255.0 blue:120/255.0 alpha:1].CGColor range:NSMakeRange(0, [SearchString length])];
    [SearchString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:width*36/640] range:NSMakeRange(0, [SearchString length])];
    long num1=-2.0f;
    CFNumberRef num = CFNumberCreate(kCFAllocatorDefault,kCFNumberCharType,&num1);
    [SearchString addAttribute:(id)kCTKernAttributeName value:(__bridge id)num range:NSMakeRange(0,[SearchString length])];
        [self.CancelButton setAttributedTitle:SearchString forState:UIControlStateNormal];
    self.CancelButton.contentHorizontalAlignment=UIControlContentHorizontalAlignmentRight;
    self.CancelButton.translatesAutoresizingMaskIntoConstraints=NO;
    NSLayoutConstraint *topConstrainforCancelButton =[NSLayoutConstraint constraintWithItem:self.CancelButton
                                                                                 attribute:NSLayoutAttributeTop
                                                                                 relatedBy:NSLayoutRelationEqual
                                                                                    toItem:self.view
                                                                                 attribute:NSLayoutAttributeTop
                                                                                multiplier:1.0f
                                                                                  constant:width*50/640];
    NSLayoutConstraint *bottomConstrainforCancelButton =[NSLayoutConstraint constraintWithItem:self.CancelButton
                                                                                  attribute:NSLayoutAttributeBottom
                                                                                  relatedBy:NSLayoutRelationEqual
                                                                                     toItem:self.TopBarView
                                                                                  attribute:NSLayoutAttributeBottom
                                                                                 multiplier:1.0f
                                                                                   constant:0-width*30/640];
    
    NSLayoutConstraint *leftConstrainforCancelButton =[NSLayoutConstraint constraintWithItem:self.CancelButton
                                                                                    attribute:NSLayoutAttributeLeading
                                                                                    relatedBy:NSLayoutRelationEqual
                                                                                       toItem:self.view
                                                                                    attribute:NSLayoutAttributeLeading
                                                                                   multiplier:1.0f
                                                                                     constant:width*500/640];
    
    NSLayoutConstraint *rightConstrainforCancelButton =[NSLayoutConstraint constraintWithItem:self.CancelButton
                                                                                  attribute:NSLayoutAttributeTrailing
                                                                                  relatedBy:NSLayoutRelationEqual
                                                                                     toItem:self.view
                                                                                  attribute:NSLayoutAttributeTrailing
                                                                                 multiplier:1.0f
                                                                                   constant:0-width*30/640];
    
    topConstrainforCancelButton.active=YES;
    bottomConstrainforCancelButton.active=YES;
    leftConstrainforCancelButton.active=YES;
    rightConstrainforCancelButton.active=YES;
 
    //set constrain for KeyWordLabel
    self.KeyWordLabel.text=@"常用关键词：";
    [self.KeyWordLabel setTextAlignment:NSTextAlignmentLeft];
    self.KeyWordLabel.font=[UIFont systemFontOfSize:width*26/640];
    self.KeyWordLabel.textColor=[UIColor colorWithRed:187/255.0 green:187/255.0 blue:187/255.0 alpha:1];
    
    self.KeyWordLabel.translatesAutoresizingMaskIntoConstraints=NO;
    NSLayoutConstraint *topConstrainforKeywordLabel =[NSLayoutConstraint constraintWithItem:self.KeyWordLabel
                                                                                  attribute:NSLayoutAttributeTop
                                                                                  relatedBy:NSLayoutRelationEqual
                                                                                     toItem:self.view
                                                                                  attribute:NSLayoutAttributeTop
                                                                                 multiplier:1.0f
                                                                                   constant:width*148/640];
    
    NSLayoutConstraint *heightConstrainforKeywordLabel =[NSLayoutConstraint constraintWithItem:self.KeyWordLabel
                                                                                  attribute:NSLayoutAttributeHeight
                                                                                  relatedBy:NSLayoutRelationEqual
                                                                                     toItem:nil
                                                                                  attribute:NSLayoutAttributeNotAnAttribute
                                                                                 multiplier:1.0f
                                                                                   constant:width*25/640];
    
    NSLayoutConstraint *leftConstrainforKeywordLabel =[NSLayoutConstraint constraintWithItem:self.KeyWordLabel
                                                                                  attribute:NSLayoutAttributeLeading
                                                                                  relatedBy:NSLayoutRelationEqual
                                                                                     toItem:self.view
                                                                                  attribute:NSLayoutAttributeLeading
                                                                                 multiplier:1.0f
                                                                                   constant:width*32/640];
    
    NSLayoutConstraint *rightConstrainforKeywordLabel =[NSLayoutConstraint constraintWithItem:self.KeyWordLabel
                                                                                   attribute:NSLayoutAttributeTrailing
                                                                                   relatedBy:NSLayoutRelationEqual
                                                                                      toItem:self.view
                                                                                   attribute:NSLayoutAttributeTrailing
                                                                                  multiplier:1.0f
                                                                                    constant:0];
    
    topConstrainforKeywordLabel.active=YES;
    heightConstrainforKeywordLabel.active=YES;
    leftConstrainforKeywordLabel.active=YES;
    rightConstrainforKeywordLabel.active=YES;
    
    
    
    int leading=32; //边距
    int left=24;  //button行间距
    int top=26;  //button列间距
    int basewidth=30;  //button底宽
    int baseheight=60;
    int characterwidth=27.5;  //每个字长度
    int currentx=leading;
    int currenty=194;
    for(NSString *keyword in self.KeyWordList){
        UIButton *keywordButton =[[UIButton alloc] initWithFrame:CGRectMake(currentx*width/640, currenty*width/640, (basewidth+characterwidth*keyword.length)*width/640, width*baseheight/640)];
        keywordButton.layer.cornerRadius=8.0f;
        keywordButton.layer.borderColor=[UIColor colorWithRed:0/255.0 green:203/255.0 blue:120/255.0 alpha:1].CGColor;
        keywordButton.layer.borderWidth= 1.0f;
        NSMutableAttributedString *keywordButtonString=[[NSMutableAttributedString alloc]  initWithString:keyword];
        [keywordButtonString addAttribute:NSForegroundColorAttributeName
                             value:(id)[UIColor colorWithRed:111/255.0 green:112/255.0 blue:113/255.0 alpha:1].CGColor range:NSMakeRange(0, [keyword length])];
        [keywordButtonString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:width*26/640] range:NSMakeRange(0, [keyword length])];
        [keywordButton setAttributedTitle:keywordButtonString forState:UIControlStateNormal];
        keywordButton.contentHorizontalAlignment=UIControlContentHorizontalAlignmentCenter;
        [self.view addSubview:keywordButton];
        [keywordButton setTitle:keyword forState:UIControlStateNormal];
        [keywordButton addTarget:self action:@selector(SearchKeyWord:)  forControlEvents:UIControlEventTouchUpInside];
        
        if((currentx+left+basewidth+characterwidth*keyword.length+leading)>640){
            currentx=leading;
            currenty+=baseheight+top;
        }
        else{
            currentx+=left+basewidth+characterwidth*keyword.length;
        }
    }
    
}

-(void)awakeFromNib{
    [super awakeFromNib];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)viewWillAppear:(BOOL)animated{
    
           [_ClinicSearchBar becomeFirstResponder];
      }




/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (IBAction)doback{
    [self.delegate ConfirmSearchContent:nil];
     [_ClinicSearchBar resignFirstResponder];
    [self dismissViewControllerAnimated:YES completion:^{
    }];
    
}

-(void)dismissViewControllerAnimated:(BOOL)flag completion:(void (^)(void))completion{
    UIViewController *cr=(UIViewController *)self.delegate;
        cr.tabBarController.view.center=CGPointMake(self.view.center.x-self.view.frame.size.width, cr.tabBarController.view.center.y );
    UIWindow *mainWindow = [[UIApplication sharedApplication].windows objectAtIndex:0];
        [mainWindow addSubview:cr.tabBarController.view];
    [cr.tabBarController.view addSubview:cr.view];
    [UIView animateWithDuration:0.1
                     animations:^{
                         cr.tabBarController.view.center=CGPointMake(self.view.center.x, cr.tabBarController.view.center.y );
                         self.view.center = CGPointMake(self.view.center.x+self.view.frame.size.width, self.view.center.y );}
                     completion:NULL];
}

-(void)SearchKeyWord:(id)sender{
    NSLog(@"%@",[((UIButton *)sender) titleForState:UIControlStateNormal ]);
    self.ClinicSearchBar.text=[((UIButton *)sender) titleForState:UIControlStateNormal ];
}

//seach bar delegeta
-(void) searchBarSearchButtonClicked:(UISearchBar *)searchBar{
    [self.delegate ConfirmSearchContent:self.ClinicSearchBar.text];
    [_ClinicSearchBar resignFirstResponder];
    [self dismissViewControllerAnimated:YES completion:nil];
    
}

@end
